/**
 */
;
(function(){
'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningAddCtrl', ['$scope','$modal','$state', 
	                                  '$stateParams','WarningAddService','BaseURL','Modal','WarningScreenService','FwaTabsetService','CommunityService',
	    function ($scope,$modal,$state,$stateParams,WarningAddService,baseUrl,Modal,WarningScreenService,FwaTabsetService,CommunityService) {
		$scope.serv = WarningAddService;
		var codeType=['WARN_SOURCE','WARN_SOURCE_TYPE','PROCESS_TYPE','WARN_TYPE'];
		var warnNo = null,
			noticeRequirement="1、即日起，请对本预警时段内医疗或售药过程中存"
			                 +"在的问题进行认真的自查与整改，并于七个工作日内以"
			                 +"书面形式提交自查报告，逾期未提交或不认真自查者，本中心将按相关规定进行处理。\n"
			                 +"2、自查中发现不合理费用应用列出具体清单及金额。\n"
			                 +"3、连续12个月内，已经书面预警2次，第3次又进入书面预警范围，将自动进入稽核程序。",
			staffCode=[],
			insurCode=[],
		    screen=[];
		$scope.saveDialogload=true;
		$scope.warningFrom={};
		$scope.warningFrom.date={};
		$scope.sourceList=[];
		$scope.notice = {};
		$scope.sourceTypeList=[];
        $scope.prcoessTypeFlag=false;
		$scope.processList=[];
		$scope.warnInsurCount=0;
		$scope.warnStaffCount=0;
		$scope.warnMedicalCount=0;
		$scope.warnInsurData=[];
		$scope.warnStaffData=[];
		$scope.warnMedicalData={};
		$scope.warnScreenList=[];
		$scope.medicalEditButtom='新增';
		$scope.baseData={};
		function init(){
			WarningAddService.getBaseFormParam(codeType).success(function(response){
				queryWarning();
				if(response.WARN_SOURCE){
					$scope.sourceList=response.WARN_SOURCE;
				}
				if(response.WARN_SOURCE_TYPE){
					$scope.sourceTypeList=response.WARN_SOURCE_TYPE;
				}
				if(response.PROCESS_TYPE){
					$scope.processList=response.PROCESS_TYPE;
				}
				if (response.WARN_TYPE) {
					$scope.warnTypeList = response.WARN_TYPE;
				}
				if(response.region_name){
					$scope.baseData.regionName=response.region_name[0].codeDesc;
				}
				if(response.warn_inscribe){
					$scope.baseData.warnInscribe=response.warn_inscribe[0].codeDesc;
				}
				if(response.monthFirst){
					$scope.baseData.monthFirst=response.monthFirst;
				}
				if(response.today){
					$scope.baseData.today=response.today;
				}
				if(response.monthLast){
					$scope.baseData.monthLast=response.monthLast;
				}
                if(!$scope.warningFrom.warnDate){
                	$scope.warningFrom.warnDate=$scope.baseData.today;
                }
                $scope.warningFrom.date={};
                if(!$scope.warningFrom.date.start){
                	$scope.warningFrom.date.start=$scope.baseData.monthFirst;
                }
                if(!$scope.warningFrom.date.end){
                	$scope.warningFrom.date.end=$scope.baseData.monthLast;
                }
		    });
		}
		init();
		
		//选中的医疗机构类型  默认为医疗机构
		var medicalType = "0";
		$scope.isWarnType = false; //是否显示预警业务 默认显示
		
		function queryWarning() {
			if ($stateParams.warnNo) {
				warnNo=$stateParams.warnNo;
				WarningAddService.getHcpmsWarning({warnNo:warnNo})
				.success(function(res){
						$scope.warningFrom=res.warnInfo || {};
						//赋值
						process = $scope.warningFrom.process;
				    	warnType = $scope.warningFrom.warnType;
						if(res.warnStaffList){
							$scope.warnStaffData=res.warnStaffList;
							$scope.warnStaffCount=res.warnStaffList.length;
							angular.forEach($scope.warnStaffData,function(data,index){
								 if($.inArray(data.code,staffCode) === -1){
									 staffCode.push(data.code);
								 }
							 });
				         }
						if(res.warnInsuranceList){
							$scope.warnInsurData=res.warnInsuranceList;
							$scope.warnInsurCount=res.warnInsuranceList.length;
							angular.forEach($scope.warnInsurData,function(data,index){
								if($.inArray(data.code,insurCode) === -1){
									insurCode.push(data.code);
								 }
							 });
				          }
						if(res.warnScreenList){
							$scope.warnScreenList=res.warnScreenList;
							angular.forEach($scope.warnScreenList,function(data,index){
								screen.push(data.monitorType+data.monitorNo+data.ruleName+data.ruleNo);
							 });
						 }
						if(res.notice){
							$scope.notice=res.notice;
						}
		        		if(res.warnMedicalInfo){
		        			$scope.warnMedicalData = res.warnMedicalInfo;
		        			//赋值是否是医疗机构还是药店
		        			medicalType = res.warnMedicalInfo.orgType;
		        			//如果是药店则不显示预警业务
		        			if (medicalType == "1") {
		        	        	warnType = "99";
		        	        	$scope.isWarnType = false;
		        	        }
		        			else {
		        				$scope.isWarnType = true;
		        			}
		        			
		        			$scope.warnMedicalCount = 1;
		        			$scope.medicalEditButtom = '编辑';
		        		}else{
		        			$scope.warnMedicalCount = 0;
		        			$scope.isWarnType = true;
		        		}
						setWarningInfoValue(res.warnInfo);
						setTabInfo();
						if($scope.warningFrom&&$scope.warningFrom.isCheckBack=='1'){							
							$scope.tabs=[{name:'网上筛查',status:1,index:0,url:'views/hcpms/warning/warnEdit/warningEditScreenBack_view.html'}];
						}
						//更新当前页面数据
						WarningScreenService.findPointCutTempScose(
		     				   WarningAddService.warningMarks,$stateParams.warnNo,'01',
		     				   $scope.warnMedicalData,$scope.warnStaffData,$scope.warnInsurData);
			     });
			}
			else {
				$scope.isWarnType = true; //是否显示预警业务 默认显示
				$scope.warningFrom.warnDate=$scope.baseData.today;
				$scope.warningFrom.date={};
				$scope.warningFrom.date.start=$scope.baseData.monthFirst;
				$scope.warningFrom.date.end=$scope.baseData.monthLast;
				setTabInfo();
				
//			    CommunityService.initDate("yyyy").success(function(res){
//				   year = res.formatDate;
//			    });
			    //获取预警编号
			    WarningAddService.getWarnNoticeNum().success(function(res){
				   $scope.warningFrom.warnNoBiz = res.noticeNum;
			    });
			}
		}
	   function setWarningInfoValue(info){
			$scope.warningFrom.date={};
			$scope.warningFrom.date.start=info.startDate;
			$scope.warningFrom.date.end=info.endDate;
			if($scope.warningFrom.process === null){
				$scope.warningFrom.process ='';
			}
			if($scope.warningFrom.source === null){
				$scope.warningFrom.source ='';
			}
			if($scope.warningFrom.sourceType === null){
				$scope.warningFrom.sourceType ='';
			}
			if(info.process === '02'||info.process === '04'){
				$scope.prcoessTypeFlag = true;
			}
		}
	    //存储上次选中的处理方式的值
	    var process = null;
	    //选中处理方式事件
	    $scope.isShowNotice = function() {
	    	$scope.warningFrom.warnNoBiz = $scope.warningFrom.warnNoBiz == null ? "" : $scope.warningFrom.warnNoBiz;
	    	var warnNo = "";
	    	if (process 
    			&& (process  === '02' ||process  === '04' )) {
    			warnNo = $scope.warningFrom.warnNoBiz.substring(1);
    		}
    		else {
    			warnNo = $scope.warningFrom.warnNoBiz;
    		}
	    	
	    	if ($scope.warningFrom.process === '02') {
				$scope.prcoessTypeFlag = true;
				warnNo = 'Y'+ warnNo;
			}
	    	else if ($scope.warningFrom.process === '04') {
	    		$scope.prcoessTypeFlag = true;
	    		warnNo = 'K'+ warnNo;
	    	}
	    	else {
				$scope.prcoessTypeFlag = false;
				$scope.notice= {} ;
			}
	    	
	    	process = $scope.warningFrom.process;
	    	
	    	$scope.warningFrom.warnNoBiz = warnNo;
	    }
	    //存储上次选中的预警业务的值
	    var warnType = null;
	    
	    //选中预警业务事件
	    $scope.changeWarnType = function () {
	    	$scope.warningFrom.warnNoBiz = $scope.warningFrom.warnNoBiz == null ? "" : $scope.warningFrom.warnNoBiz;
	    	var validate = "";
	    	//住院
		   if (medicalType == "0" 
		        && $scope.warningFrom.warnType == '1') {
			   validate = "Z";
		   }
		   //门诊
		   else if (medicalType == "0" 
			        && $scope.warningFrom.warnType == '2') {
			   validate = "M";
		   }
		   //药店刷卡
		   else if (medicalType == "1") {
			   $scope.warningFrom.warnType = '';
			   $scope.isWarnType = false;
			   validate = "D";
		   }
		   else {
			   $scope.isWarnType = true;
		   }
	       var warnNo = "";
	       if ($scope.warningFrom.process === '02' || $scope.warningFrom.process === '04') {
	    		if (warnType) {
	    			warnNo = $scope.warningFrom.warnNoBiz.substring(0,1) + validate + $scope.warningFrom.warnNoBiz.substring(2);
	    		}
	    		else {
	    			warnNo = $scope.warningFrom.warnNoBiz.substring(0,1) + validate + $scope.warningFrom.warnNoBiz.substring(1);
	    		}
	    	}
	    	else {
				if (warnType) {
					warnNo = validate + $scope.warningFrom.warnNoBiz.substring(1);
	    		}
	    		else {
	    			warnNo = validate + $scope.warningFrom.warnNoBiz;
	    		}
			}
	        if (medicalType == "1") {
	        	warnType = "99";
	        }
	        else {
	        	warnType = $scope.warningFrom.warnType;
	        }
	    	
	    	$scope.warningFrom.warnNoBiz = warnNo;
	    }
		/****tab页面*/
		function setTabInfo(){			
			$scope.currentTab = 0;
			$scope.currentTabIndex = 0;
			$scope.tabs=[{name:'网上筛查',status:1,index:0,url:'views/hcpms/warning/warnEdit/warningEditScreen_view.html'}];
			$scope.tabClick = function(index) {
				$scope.currentTab = index;
				$scope.currentTabIndex=index;
			}
		}
		
		//自查通知书
		$scope.warningNoticeDialog = function() {
			var scope = $scope.$new();
			if(!$scope.notice.noticeNo){
				setNoticWarnNo();
			}
			if(!$scope.notice||!$scope.notice.name){
				$scope.notice.name =$scope.warnMedicalData.name;
			}
			if(!$scope.notice||!$scope.notice.datestart){
				$scope.notice.datestart =$scope.warningFrom.date.start;
			}
            if(!$scope.notice||!$scope.notice.dateend){
				$scope.notice.dateend=$scope.warningFrom.date.end;
			}
			if(!$scope.notice||!$scope.notice.content){
				$scope.notice.content=$scope.warningFrom.reason;
			}
			if(!$scope.notice||!$scope.notice.requirement){
				$scope.notice.requirement=noticeRequirement;
			}
			if(!$scope.notice.noticeDate){
				$scope.notice.noticeDate = $scope.baseData.today;
			}
			if(!$scope.notice.regionName){
				$scope.notice.regionName=$scope.baseData.regionName;
			}
			if(!$scope.notice.warnInscribe){
				$scope.notice.warnInscribe=$scope.baseData.warnInscribe;
			}
			scope.warnNotice=$scope.notice;
			var modalInstance = $modal.open({
	      	      templateUrl: baseUrl +'views/hcpms/warning/warnEdit/warningEditNoticeDialog_view.html',
	      	      backdrop:'static',
				  size:'sm',
				  scope:scope,
	      	      controller:'WarningAddNoticeCtrl'	      	      
		      	});
			modalInstance.result.then(function(item) {
				$scope.notice=item;
			});
		}
	    $scope.$watch('warningFrom.warnNoBiz',function(v){
			 setNoticWarnNo();
        });
		 function setNoticWarnNo(){
			var str="";
			if(medicalType == "1"){
				str="预警药字";
			}else{
				str="预警医字";
			}
			if($scope.warningFrom.warnNoBiz){
	          str=str+$scope.warningFrom.warnNoBiz+"号"
			}else{
				str=str+"号";
			}
			$scope.notice.noticeNo=str;
		}
//		function getPointAllData(){
//			WarningAddService.getPointAllData({caseNo:warnNo,caseType:'01'})
//				.success(function(res){
//				if(res.pointCutTemps){
//					angular.forEach(res.pointCutTemps,function(data,index){
//						if(data.targetType==='01'){
//							addMedicalScore(data);
//						}else if(data.targetType==='02'){
//							addStaffScore(data);
//						}else if(data.targetType==='03'){
//							addInsurScore(data);
//						}
//					});
//				}
//			});
//		}
//		function addMedicalScore(data){
//			if(!$scope.warnMedicalData.score){
//				$scope.warnMedicalData.score=0;
//			}
//			if(!$scope.warnMedicalData.scoreData){
//				$scope.warnMedicalData.scoreData=[];
//			}
//			$scope.warnMedicalData.score=parseFloat($scope.warnMedicalData.score)+parseFloat(data.point);
//			var score={};
//			score.point=parseFloat(data.point).toFixed(2);
//			score.reason=data.reason;
//			$scope.warnMedicalData.scoreData.push(score);
//			$scope.warnMedicalData.show=false;
//			$scope.warnMedicalData.score=parseFloat($scope.warnMedicalData.score).toFixed(2);
//		}
//		function addStaffScore(score){
//			angular.forEach($scope.warnStaffData,function(data,index){
//				if(!data.score){
//					data.score=0;
//				}
//				if(!data.scoreData){
//					data.scoreData=[];
//				}
//				data.score=parseFloat(data.score)+parseFloat(score.point);
//				var scoreData={};
//				scoreData.point=parseFloat(score.point).toFixed(2);
//				scoreData.reason=score.reason;
//				data.scoreData.push(scoreData);
//				data.show=false;
//				data.score=parseFloat(data.score).toFixed(2);
//			});
//		}
//		function addInsurScore(score){
//			angular.forEach($scope.warnInsurData,function(data,index){
//				if(!data.score){
//					data.score=0;
//				}
//				if(!data.scoreData){
//					data.scoreData=[];
//				}
//				data.score=parseFloat(data.score)+parseFloat(score.point);
//				var scoreData={};
//				scoreData.point=parseFloat(score.point).toFixed(2);
//				scoreData.reason=score.reason;
//				data.scoreData.push(scoreData);
//				data.show=false;
//				data.score=parseFloat(data.score).toFixed(2);
//			});
//		}
//		function changeScore(data,scoreDatas){
//				data.score=0.00;
//				data.scoreData=[];
//				angular.forEach(scoreDatas,function(score,index){
//					data.score=parseFloat(data.score)+parseFloat(score.point);
//					var scoreData={};
//					scoreData.point=parseFloat(score.point).toFixed(2);
//					scoreData.reason=score.reason;
//					data.scoreData.push(scoreData);
//				});
//				data.score=parseFloat(data.score).toFixed(2);
//		}
		//新增参保人员
		$scope.addInsurance = function() {
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningAddInsurDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope: $scope,
				  controller: 'WarningAddInsurCtrl'
			    });
			 modalInstance.result.then(function(item) {
				 angular.forEach(item,function(data,index){
					 if($.inArray(data.code, insurCode) === -1){
						 $scope.warnInsurData.push(data);
						 insurCode.push(data.code);
					 }
				 });
				 $scope.warnInsurCount=$scope.warnInsurData.length;
	 	    });
		}
		//删除参保人员
		$scope.deleteInsurance = function (data){
			Modal.confirm('确认删除参保人'+data.name+'吗？',function(){
				 WarningScreenService.deleteMark(WarningAddService.warningMarks,3,data);//mark:1.机构，2.医务人员，3.参保人
				 $scope.warnInsurData.splice($.inArray(data,$scope.warnInsurData),1);
				 insurCode.splice($.inArray(data.code,insurCode),1);
				 $scope.warnInsurCount=$scope.warnInsurData.length;
			});
		}
		//新增医护人员
		$scope.addStaff = function() {
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningAddStaffDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  controller: 'WarningAddStaffCtrl'
			    });
			 //接收返回数据
			 modalInstance.result.then(function(item) {
				 angular.forEach(item,function(data,index){
					 if($.inArray(data.code, staffCode) === -1){
						 $scope.warnStaffData.push(data);
						 staffCode.push(data.code);
					 }
			     });
				 $scope.warnStaffCount=$scope.warnStaffData.length;
	 	    });
		}
		//删除医护人员
		$scope.deleteStaff = function (data){
			Modal.confirm('确认删除医护人员'+data.name+'吗？',function(){
				WarningScreenService.deleteMark(WarningAddService.warningMarks,2,data);//mark:1.机构，2.医务人员，3.参保人
				$scope.warnStaffData.splice($.inArray(data,$scope.warnStaffData),1);
				staffCode.splice($.inArray(data.code,staffCode),1);
				$scope.warnStaffCount=$scope.warnStaffData.length;
			});
		}
		
		//新增医疗机构
		$scope.addMedical = function() {
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningAddMedicalDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  controller: 'WarningAddMedicalCtrl'
			    });
			 //接收返回数据
			 modalInstance.result.then(function(item) {
				 if($scope.warnMedicalData){//删除
						WarningScreenService.deleteMark(WarningAddService.warningMarks,1,$scope.warnMedicalData,$scope.warnStaffData,$scope.warnInsurData);//mark:1.机构，2.医务人员，3.参保人
				 }
			     $scope.warnMedicalData = item;
			     $scope.notice.name =item.name;
			     medicalType = item.orgType;
			     $scope.changeWarnType();
				 $scope.warnMedicalCount = 1;
				 $scope.medicalEditButtom='编辑';
	 	    });
		}
		//查看参保人员详细信息
		$scope.viewInsurList = function(){
			if(!$scope.warnInsurData&&$scope.warnInsurData.length===0){
				return;
			}
			var scope = $scope.$new();
			scope.warnInsuranceList=$scope.warnInsurData;
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnView/warningViewInsurance_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller:function($modalInstance,$scope){
					  $scope.onOk = function(){
				  		   $modalInstance.dismiss();
				  	   }	
				  }
			    });
			
		}
		//查看医护人员详细信息
		$scope.viewStaffList = function(){
            if(!$scope.warnStaffData&&$scope.warnStaffData.length===0){
            	return;
            }
			var scope = $scope.$new();
			scope.warnStaffList=$scope.warnStaffData;
			var modalInstance = $modal.open({
				  templateUrl: baseUrl+'views/hcpms/warning/warnView/warningViewStaff_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller:function($scope,$modalInstance){
		  	          //取消或或者直接关闭
					  $scope.onOk = function(){
						  $modalInstance.dismiss();
			          }	
				  }
			    });
			
		}
		//查看医疗机构详细信息
		$scope.viewMedicalInfo = function(){
            if(!$scope.warnMedicalData){
            	return;
            }
			var scope = $scope.$new();
			$scope.medical=$scope.warnMedicalData;
			var modalInstance = $modal.open({
				  templateUrl: baseUrl+'views/hcpms/warning/warnView/warningViewMedical_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller:function($scope,$modalInstance){
		  	          //取消或或者直接关闭
					  $scope.onOk = function(){
						  $modalInstance.dismiss();
			          }	
				  }
			    });
			
		}
		//隐藏扣分以及删除按钮
		$scope.mouseoutShow = function(data){
			data.show = false;
		}
		
        //显示扣分以及删除按钮
		$scope.mouseoverShow = function(data){
			data.show = true;
		}
		
		//新增网上筛查
		$scope.addScreen = function() {
			//打开弹出框
			var modalInstance = $modal.open({
	      	      templateUrl: baseUrl + 'views/hcpms/warning/warnEdit/warningAddScreenDialog_view.html',
	      	      backdrop:'static',
				  size:'sm',
	      	      controller:'WarningAddScreenCtrl'
		      	});
			 //接收返回数据
			 modalInstance.result.then(function(item) { 
				 if($.inArray(item.monitorType+item.monitorNo+item.ruleName+item.ruleNo, screen) != -1){
					 return;
				 }
				 screen.push(item.monitorType+item.monitorNo+item.ruleName+item.ruleNo);
				 $scope.warnScreenList.push(item);
	  	    });
		}
		//全选或者不全选
		$scope.checkRows = [];
		$scope.checked = false;
		$scope.getWarnScreenIdList = function (checked){
			angular.forEach($scope.warnScreenList,function(data,index){
				data.checked = checked;
			});
		}
		//删除网上筛查
		$scope.deleteScreen = function() {
			if(!$scope.warnScreenList){
				return;
			}
			var checkFlag = null;
			$.each($scope.warnScreenList, function(){
				if (this.checked===true) {
					checkFlag = 'true';
				}
			});
			if(!checkFlag){
				   Modal.alert('请选择需要删除的选项！');
				   return;
			}
			Modal.confirm('确认删除网上筛查吗？',function(){
				var data=[];
				$.each($scope.warnScreenList, function(){
					if (this.checked===true) {
						screen.splice($.inArray(this.monitorType+this.monitorNo+this.ruleName+this.ruleNo,screen),1);						
					}else{
						data.push(this);
					}
				});
				$scope.warnScreenList=data;
			});
		}
		//取消
		 $scope.backWarning = function(){
			   	WarningAddService.warningMarks = WarningAddService.warningMarkTemps;//初始化扣分参数
			   	if($stateParams.urlFlag){
			   		var urlFlag=$stateParams.urlFlag;
					FwaTabsetService.closeTab('warnQueryAdd','workingArea.warnTransactorle',{urlFlag:urlFlag});
			   	}else{
		   			FwaTabsetService.selectTab("warnQueryAdd.warningAddList",null,false);
			   	}
	        }
	   
		   //暂存
		   $scope.saveWarning = function() {
			   //验证预警编号是否正确
			   if (!warningWorkValidate()) {
				   return false;
			   }
//			   if(!isWarningNull()){
//				  return;
//			   }
//			   if(!$scope.warningFrom.warnNoBiz){   
//				   Modal.alert("预警编号不能为空!");
//				   return false;
//			   }
//			   var condition=/^[a-zA-Z]{2}\d{11}$/;
//			   if(!condition.test($scope.warningFrom.warnNoBiz)){
//				   Modal.alert("预警编号格式应为13位(前2位字母,后11位数字)!");
//				   return false;
//			   }			  
	           var params=getParams();
	           $scope.saveDialogload = true;
			   WarningAddService.saveHcpmsWarning(params).success(function(data){
				   $scope.saveDialogload = false;
				   Modal.alert(data.msg);
				   if(data.state == '0'){
					   WarningAddService.warningMarks = [];//初始化扣分参数
					   if(data.warnNo) {
						   FwaTabsetService.selectTab("warnQueryAdd.warningEdit",{warnNo:data.warnNo},false);                  
		               }
					   else{
		            	   queryWarning(); 
		               }
				   }
			   });
		   }
		   
		   
		   //验证预警通知单号是否正确
		   function warningWorkValidate() {
			   if (!$scope.warningFrom.warnNoBiz) {   
				   Modal.alert("预警编号不能为空!");
				   return false;
			   }
			   if (!$scope.warningFrom.process) {   
				   Modal.alert("处理方式不能为空!");
				   return false;
			   }
			   if (medicalType == "0" && !$scope.warningFrom.warnType) {   
				   Modal.alert("预警业务不能为空!");
				   return false;
			   }
			   var twoWarnNoBiz =  "",twoName = "二";
			   //存储医保年度
			   var warnNoYear = "";
			   
			   //判断长度
			   if ($scope.warningFrom.process == '02' 
				   || $scope.warningFrom.process == '04') {
				   var condition=/^[A-Z]{2}\d{11}$/;
				   if(!condition.test($scope.warningFrom.warnNoBiz)){
					   Modal.alert("预警编号格式必须为13位字符(前2位为大写字母,后11位数字)!");
					   return false;
				   }
				   warnNoYear = $scope.warningFrom.warnNoBiz.substring(2,6);
			   }
			   else {
				   var condition=/^[A-Z]{1}\d{11}$/;
				   if(!condition.test($scope.warningFrom.warnNoBiz)){
					   Modal.alert("预警编号格式必须为12位字符(前1位为大写字母,后11位数字)!");
					   return false;
				   }
				   warnNoYear = $scope.warningFrom.warnNoBiz.substring(1,5);
			   }
			   //书面
			   if ($scope.warningFrom.process == '02') {
				   var validate=/^[Y]$/;
				   var oneWarnNoBiz = $scope.warningFrom.warnNoBiz.substring(0,1);
				   if (!validate.test(oneWarnNoBiz)) {
					   Modal.alert("处理方式为“书面”预警编号["+$scope.warningFrom.warnNoBiz+"]第一个字母必须为大写Y!");
					   return false;
				   }
				   twoWarnNoBiz = $scope.warningFrom.warnNoBiz.substring(1,2);
				   twoName = "二";
			   }
			   //约谈
			   else if ($scope.warningFrom.process == '04') {
				   var validate=/^[K]$/;
				   var oneWarnNoBiz = $scope.warningFrom.warnNoBiz.substring(0,1);
				   if (!validate.test(oneWarnNoBiz)) {
					   Modal.alert("处理方式为“约谈”预警编号["+$scope.warningFrom.warnNoBiz+"]第一个字母必须为大写K!");
					   return false;
				   }
				   twoWarnNoBiz = $scope.warningFrom.warnNoBiz.substring(1,2);
				   twoName = "二";
			   }
			   else {
				   twoWarnNoBiz = $scope.warningFrom.warnNoBiz.substring(0,1);
				   twoName = "一";
			   }
			   
			   //住院
			   if (medicalType == "0" 
				   && $scope.warningFrom.warnType == '1') {
				   var validate=/^[Z]$/;
				   if (!validate.test(twoWarnNoBiz)) {
					   Modal.alert("预警业务为“住院”预警编号["+$scope.warningFrom.warnNoBiz+"]第"+twoName+"个字母必须为大写Z!");
					   return false;
				   }
			   }
			   //门诊
			   else if (medicalType == "0" 
				   && $scope.warningFrom.warnType == '2') {
				   var validate=/^[M]$/;
				   if (!validate.test(twoWarnNoBiz)) {
					   Modal.alert("预警业务为“门诊”预警编号["+$scope.warningFrom.warnNoBiz+"]第"+twoName+"个字母必须为大写M!");
					   return false;
				   }
			   }
			   //药店刷卡
			   else if (medicalType == "1") {
				   var validate=/^[D]$/;
				   if (!validate.test(twoWarnNoBiz)) {
					   Modal.alert("预警业务为“药店刷卡”预警编号["+$scope.warningFrom.warnNoBiz+"]第"+twoName+"个字母必须为大写D!");
					   return false;
				   }
			   }
			   //医保年份校验
			   var yearValidate=/^20\d{2}$/;
			   if (!yearValidate.test(warnNoYear)) {
				   Modal.alert("预警编号["+$scope.warningFrom.warnNoBiz+"]医保年度“"+warnNoYear+"”请修改成2000以上的年份!");
				   return false;
			   }
			   return true;
		   }
		   
		   //验证信息
		   function isWarningNull(){
			   var dateReg =/^\d{4}-\d{2}-\d{2}$/; 
			   //验证预警编号是否正确
			   if (!warningWorkValidate()) {
				   return false;
			   }
			   
			   if(!dateReg.test($scope.warningFrom.warnDate)){   
				   Modal.alert("受理日期不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)！");
				   return false;
			   }
//			   if(!$scope.warningFrom.warnNoBiz){   
//				   Modal.alert("预警编号不能为空!");
//				   return false;
//			   }
//			   var condition=/^[a-zA-Z]{2}\d{11}$/;
//			   if(!condition.test($scope.warningFrom.warnNoBiz)){
//				   Modal.alert("预警编号格式应为13位(前2位字母,后11位数字)!");
//				   return false;
//			   }			  
			   if(!$scope.warningFrom.source){   
				   Modal.alert("案件来源不能为空!");
				   return false;
			   }
			   if(!$scope.warningFrom.sourceType){   
				   Modal.alert("来源方式不能为空!");
				   return false;
			   }
			   if(!$scope.warningFrom.sourceObject){   
				   Modal.alert("来源对象不能为空!");
				   return false;
			   }
			   if(!$scope.warningFrom.summary){   
				   Modal.alert("预警原因不能为空!");
				   return false;
			   }
			   var date = $scope.warningFrom.date;
			   if(!dateReg.test(date.start)||!dateReg.test(date.end)){   
				   Modal.alert("预警时段不能为空,并且必须输入正确的日期格式(yyyy-MM-dd)！");
				   return false;
			   }
			   
			   if(!$scope.warningFrom.reason){   
				   Modal.alert("预警内容不能为空!");
				   return false;
			   }
//			   if(!$scope.warningFrom.process){   
//				   Modal.alert("处理方式不能为空!");
//				   return false;
//			   }
			   if(!$scope.warnMedicalData){
				   Modal.alert("缺少医疗机构,请增加医疗机构!");
				   return false;
			   }
			   if(!$scope.warnMedicalData.code||!$scope.warnMedicalData.name){
				   Modal.alert("缺少医疗机构,请增加医疗机构!");
				   return false;
			   }
			   if($scope.warningFrom.process === '02'||$scope.warningFrom.process === '04'){
				   if(!$scope.notice){
					   //Modal.alert("处理方式为自查整改时，请填写预警通知单!");
					   Modal.alert("请填写预警通知单!");
					   return false;
				   }
				   if(!$scope.notice.content||!$scope.notice.datestart||!$scope.notice.dateend||!$scope.notice.requirement){
					   //Modal.alert("处理方式为自查整改时，没有填写预警通知单或没有填写完全,请完善!");
					   Modal.alert("没有填写预警通知单或没有填写完全,请完善!");
					   return false;
				   }
				   if($scope.notice.name!=$scope.warnMedicalData.name){
					   $scope.notice.name=$scope.warnMedicalData.name
				   }
			   }else{
				   $scope.notice={};
			   }
			   return true;
		   }
		   //保存和暂存设置保存的内容
		   function getParams(){
				   var date = $scope.warningFrom.date;
		           var medicalInfo={},
				      staffList=[],
				      insurList=[],
				      screenList=[];
		           if($scope.warnScreenList){
		        	   screenList=$scope.warnScreenList;
		           }
				   if($scope.warnMedicalData.code){
					   medicalInfo.code=$scope.warnMedicalData.code;
				   }
				   if($scope.warnMedicalData.name){
					   medicalInfo.name=$scope.warnMedicalData.name;
				   }
				   angular.forEach($scope.warnStaffData,function(data,index) {
					   var staff={};
					   if(data.code){
						   staff.code = data.code;
					   }
					   if(data.name){
						   staff.name = data.name;
					   }
					   staffList.push(staff);
				   });
				   angular.forEach($scope.warnInsurData,function(data,index) {
					   var insurance={};
					   if(data.code){
						   insurance.code = data.code;
					   }
					   if(data.name){
						   insurance.name = data.name;
					   }
					   insurList.push(insurance);
				   });
		           var param={
		        		     warnNo: warnNo,
		        		     warnDate: $scope.warningFrom.warnDate,
		        		     source: $scope.warningFrom.source,
		        		     sourceType: $scope.warningFrom.sourceType,
		        		     startDate: date.start,
		        		     endDate: date.end,
		        		     reason: $scope.warningFrom.reason,
		        		     process: $scope.warningFrom.process,
		        		     sourceObject: $scope.warningFrom.sourceObject,
		        		     contact: $scope.warningFrom.contact,
		        		     phone: $scope.warningFrom.phone,
		        		     mail: $scope.warningFrom.mail,
		        		     address: $scope.warningFrom.address,
		        		     summary: $scope.warningFrom.summary,
		        		     warnNoBiz: $scope.warningFrom.warnNoBiz,
		        		     warnType : $scope.warningFrom.warnType,
		        		     status: $scope.warningFrom.status
		                     };
		           var params={
		        		      warnNo:warnNo,
		        		      status:$scope.warningFrom.status,
		        		      warnInfo:param,
		        		      screenList:screenList,
		        		      medicalInfo:medicalInfo,
		        		      staffList:staffList,
		        		      insurList:insurList,
		        		      notice:$scope.notice,
		        		      warnMarkTempDetails:WarningAddService.warningMarks
		                        }; 
		          return params;
		   }
		   //保存
		   $scope.submitWarning = function(){
			   if(!isWarningNull()){
				  return;
			   }		  
	           var params=getParams();
	           params.warnInfo.isCheckBack="0";
	           $scope.saveDialogload = true;
	           WarningAddService.submitWarnProcess(params).success(function(data){
	        	   $scope.saveDialogload = false;
	        	   Modal.alert(data.msg);
				   if(data.state=='0') {
					   WarningAddService.warningMarks = WarningAddService.warningMarkTemps;//初始化扣分参数
					   if($stateParams.urlFlag){
						   var urlFlag=$stateParams.urlFlag;
							FwaTabsetService.closeTab('warnQueryAdd','workingArea.warnTransactorle',{urlFlag:urlFlag});
					   }else{
						   FwaTabsetService.selectTab("warnQueryAdd.warningAddList",null,false); 
					   }
					                    
	               }
			   });
		   }
		   
		   //扣分 mark：1.机构扣分，2.医务人员扣分，3参保人员扣分,data:数据集合
		   $scope.marking = function(mark,data,medicalCode,orgType){
			   var medicalCodeTemp=null;
				if(medicalCode&&!angular.isUndefined(medicalCode)){
					medicalCodeTemp = medicalCode;
				}
			   if(mark != 1 && medicalCodeTemp == null){
				   Modal.alert("请先增加医疗机构信息！");
					return null;
			   }else{
				   medicalCode = medicalCodeTemp;
			   }
			   markData(mark,data,medicalCode,orgType);//扣分数据参数加载
			   var modalInstance = $modal.open({
				      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningDeductionDialog_view.html',
				      backdrop:'static',
					  size:'sm',
					  scope: $scope,
//					  scope:scope,
					  controller: 'WarningDeductionDialogCtrl'
				    });
				 //接收返回数据
				 modalInstance.result.then(function(item) {
					 if (!WarningAddService.warningMarks) {
						 WarningAddService.warningMarks = [];
						}
					 WarningAddService.warningMarks = item;
					//更新当前页面数据
					 WarningScreenService.findPointCutTempScose(item,$stateParams.warnNo,'01',
							   $scope.warnMedicalData,$scope.warnStaffData,$scope.warnInsurData); 
		 	    });
		   }
		   //扣分数据参数加载
		   var markData = function(mark,data,medicalCode,orgType){
			   var par = getParams(); 
			   WarningAddService.mark = mark;
				   WarningAddService.warningMarkData = {
						   orgType:orgType,//0.机构，1.药店
						   medicalCode:medicalCode,//medicalCode:所属机构编号
						   code:data.code,//code:编号
						   name:data.name,//name:名称
						   caseType:'01',//案件类型caseType：01.预警,02.稽核
						   caseNo:$stateParams.warnNo,//案件编号
						   happenDate:par.warnInfo.warnDate,//案发日期
						   punishDateStart:par.warnInfo.startDate,// 惩罚起期
						   punishDateEnd:par.warnInfo.endDate,// 惩罚终期
						   warningMarks:WarningAddService.warningMarks//扣分项数据
						   };
		   }
		   $scope.saveDialogload=false;
	}]);
})();